namespace ETestIT.Core.Services
{
    using System.Collections.Generic;
    using DataInterfaces;

    public class CandidateResumeServiceImpl : ICandidateResumeService
    {
        private readonly ICandidateResumeRepository candidateResumeRepository;

        public CandidateResumeServiceImpl(
            ICandidateResumeRepository candidateResumeRepository)
        {
            this.candidateResumeRepository = candidateResumeRepository;
        }

        #region ICandidateResumeService Members

        public void Add(Candidate candidate, JobTitle jobTitle, Resume resume)
        {
            var candidateResume = new CandidateResume
                                      {
                                          TheResume = resume,
                                          TheJobTitle = jobTitle,
                                          TheCandidate = candidate
                                      };

            resume.Version = 1;
            candidateResumeRepository.SaveOrUpdate(candidateResume);
        }

        public void Update(int previosResumeId, Resume resume)
        {
            var previosCandidateResume = candidateResumeRepository.GetByResumeId(previosResumeId);

            resume.Version = previosCandidateResume.TheResume.Version + 1;

            var candidateResume = new CandidateResume {
                  TheCandidate = previosCandidateResume.TheCandidate,
                  TheJobTitle = previosCandidateResume.TheJobTitle,
                  TheResume = resume
              };

            candidateResumeRepository.SaveOrUpdate(candidateResume);
        }

        public void Delete(int resumeId)
        {
            CandidateResume candidateResume = candidateResumeRepository.Get(resumeId);
            candidateResumeRepository.Delete(candidateResume);
        }

        public IList<CandidateResume> GetLatestVersionsResume(int candidateId)
        {
            return candidateResumeRepository.GetLatestVersions(candidateId);
        }

        public IList<Resume> GetHistoryByCanditadeResumeId(int candidateResumeId)
        {
            return candidateResumeRepository.GetResumesHistoryBy(candidateResumeId);
        }

        #endregion
    }
}